package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.chactVo.ChapterVo;
import com.atguigu.eduservice.service.EduChapterService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author lhl
 * @since 2022-07-04
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
@Slf4j
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;
    // 获取章节列表
    @ApiOperation(value = "嵌套章节数据列表")
    @GetMapping("chapterlist/{courseId}")
    public R nestedListByCourseId(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId){

        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
        return R.ok().data("items", chapterVoList);
    }
    // 添加章节
    @PostMapping("/saveChapter")
    public R save(@RequestBody EduChapter chapter){
        log.info("添加方法被执行");
        chapterService.save(chapter);
        log.info("数据为::"+chapter);
        return R.ok();
    }
    // 根据id查询章节
    @GetMapping("getChapter/{id}")
    public R getChapter(@PathVariable String id){
        return R.ok().data("item",chapterService.getById(id));
    }
    // 根据id修改
    @PostMapping("/updateChapter")
    public R updateById(@RequestBody EduChapter chapter){
//        System.out.println("执行了修改方");
        log.info("修改方法被执行");
        chapterService.updateById(chapter);
        return R.ok();
    }
    // 根据id删除
    @GetMapping("deleteChapter/{id}")
    public R deleteChapter(@PathVariable String id){
        if(chapterService.deleteChapter(id)){
            return R.ok();
        }else{
            return R.error();
        }
    }
}

